package me.flyray.bsin.server.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.constants.ResponseCode;
import me.flyray.bsin.context.BsinServiceContext;
import me.flyray.bsin.domain.entity.AiAgent;
import me.flyray.bsin.domain.entity.LLMParam;
import me.flyray.bsin.exception.BusinessException;
import me.flyray.bsin.facade.service.AiAgentService;
import me.flyray.bsin.infrastructure.mapper.AiAgentMapper;
import me.flyray.bsin.infrastructure.mapper.CopilotMapper;
import me.flyray.bsin.security.contex.LoginInfoContextHelper;
import me.flyray.bsin.security.domain.LoginUser;
import me.flyray.bsin.server.utils.Pagination;
import me.flyray.bsin.utils.BsinSnowflake;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.apache.dubbo.annotation.ShenyuDubboService;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@ShenyuDubboService(path = "/aiAgent", timeout = 6000)
@ApiModule(value = "aiAgent")
@Service
@Slf4j
public class AiAgentServiceImpl implements AiAgentService {

    @Autowired
    private AiAgentMapper aiAgentMapper;

    @ApiDoc(desc = "add")
    @ShenyuDubboClient("/add")
    @Override
    public AiAgent add(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        String merchantNo = loginUser.getMerchantNo();
        String tenantId = MapUtils.getString(requestMap, "tenantId");
        if (tenantId == null) {
            tenantId = loginUser.getTenantId();
            if (tenantId == null) {
                throw new BusinessException(ResponseCode.TENANT_ID_NOT_ISNULL);
            }
        }
        AiAgent aiAgent = BsinServiceContext.getReqBodyDto(AiAgent.class, requestMap);
        aiAgent.setSerialNo(BsinSnowflake.getId());
        aiAgent.setTenantId(tenantId);
        aiAgent.setMerchantNo(merchantNo);
        aiAgentMapper.insert(aiAgent);
        return aiAgent;
    }

    @ApiDoc(desc = "delete")
    @ShenyuDubboClient("/delete")
    @Override
    public void delete(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        aiAgentMapper.deleteById(serialNo);
    }

    @ApiDoc(desc = "edit")
    @ShenyuDubboClient("/edit")
    @Override
    public void edit(Map<String, Object> requestMap) {

    }

    @ApiDoc(desc = "getDetail")
    @ShenyuDubboClient("/getDetail")
    @Override
    public AiAgent getDetail(Map<String, Object> requestMap) {
        return null;
    }

    @ApiDoc(desc = "createDigitalAvatarOrBrandOfficer")
    @ShenyuDubboClient("/createDigitalAvatarOrBrandOfficer")
    @Override
    public AiAgent createDigitalAvatarOrBrandOfficer(Map<String, Object> requestMap) {
        return null;
    }

    @ApiDoc(desc = "getPageList")
    @ShenyuDubboClient("/getPageList")
    @Override
    public IPage<AiAgent> getPageList(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Object paginationObj =  requestMap.get("pagination");
        Pagination pagination = new Pagination();
        BeanUtil.copyProperties(paginationObj,pagination);

        Page<AiAgent> page = new Page<>(pagination.getPageNum(), pagination.getPageSize());
        LambdaQueryWrapper<AiAgent> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AiAgent::getCreateTime);
        wrapper.eq(AiAgent::getTenantId, tenantId);
        IPage<AiAgent> pageList = aiAgentMapper.selectPage(page, wrapper);
        return pageList;
    }

    @ApiDoc(desc = "getList")
    @ShenyuDubboClient("/getList")
    @Override
    public List<AiAgent> getList(Map<String, Object> requestMap) {
        return null;
    }
    
}
